﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Specialized;

namespace SL8.SL.Extensions
{
    public static class PanelService
    {
        #region 模版
        private static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.RegisterAttached("ItemTemplate",
            typeof(DataTemplate),
            typeof(PanelService),
            new PropertyMetadata(null, new PropertyChangedCallback(PanelService.ItemTemplatePropertyChanged)));

        public static DataTemplate GetItemTemplate(DependencyObject d)
        {
            return (DataTemplate)d.GetValue(PanelService.ItemTemplateProperty);
        }

        public static void SetItemTemplate(DependencyObject d, DataTemplate value)
        {
            d.SetValue(PanelService.ItemTemplateProperty, value);
        }

        private static void ItemTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #region 数据源
        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource",
            typeof(IEnumerable),
            typeof(PanelService),
            new PropertyMetadata(new PropertyChangedCallback(PanelService.ItemsSourceChanged)));

        public static IEnumerable GetItemsSource(DependencyObject d)
        {
            return (IEnumerable)d.GetValue(PanelService.ItemsSourceProperty);
        }

        public static void SetItemsSource(DependencyObject d, IEnumerable value)
        {
            d.SetValue(PanelService.ItemsSourceProperty, value);
        }

        private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Panel panel = d as Panel;
            if (panel == null)
            {
                return;
            }

            DataTemplate dataTemplate = PanelService.GetItemTemplate(d);
            if (dataTemplate == null)
            {
                return;
            }

            panel.Children.Clear();
            IEnumerable enumerable = e.NewValue as IEnumerable;
            if (enumerable != null)
            {
                foreach (var dataItem in enumerable)
                {
                    ContentControl content = new ContentControl();
                    content.Content = dataItem;
                    content.ContentTemplate = dataTemplate;
                    panel.Children.Add(content);
                }
            }

            if (e.NewValue is INotifyCollectionChanged)
            {
                INotifyCollectionChanged notifyCollection = (INotifyCollectionChanged)e.NewValue;
                notifyCollection.CollectionChanged += (sender, arg) =>
                {
                    if (arg.Action == NotifyCollectionChangedAction.Add)
                    {
                        foreach (var dataItem in arg.NewItems)
                        {
                            ContentControl content = new ContentControl();
                            content.Content = dataItem;
                            content.ContentTemplate = dataTemplate;
                            panel.Children.Add(content);
                        }

                    }
                    else if (arg.Action == NotifyCollectionChangedAction.Remove)
                    {

                    }
                };
            }

            //if (e.OldValue is INotifyCollectionChanged)
            //{
            //    INotifyCollectionChanged notifyCollection = (INotifyCollectionChanged)e.NewValue;
            //}
        }
        #endregion
    }
}
